package com.ibeeking.found.cop.b.rest.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.ibeeking.found.common.utils.UserUtils;
import com.ibeeking.found.cop.b.rest.service.IGroupAutoTagsService;
import com.ibeeking.found.cop.service.common.bo.GroupAutoTagsRuleBO;
import com.ibeeking.found.cop.service.common.dos.GroupTagsDO;
import com.ibeeking.found.cop.service.common.param.GroupAutoTagsParam;
import com.ibeeking.found.cop.service.common.query.GroupAutoTagsPageQuery;
import com.ibeeking.found.cop.service.common.query.GroupAutoTagsQuery;
import com.ibeeking.found.cop.service.common.vo.GroupAutoTagsVO;
import com.ibeeking.found.cop.service.mapper.GroupTagsMapper;
import com.ibeeking.found.id.GenerateId;
import com.ibeeking.nematos.exception.BusinessException;
import com.ibeeking.nematos.utils.bean.BeanUtil;
import com.ibeeking.nematos.utils.data.Collections3;
import com.ibeeking.nematos.utils.json.JsonUtils;
import org.apache.commons.lang3.StringUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.time.LocalDateTime;
import java.util.List;

/**
 * @ClassName GroupAutoTagsServiceImpl
 * @Description
 * @Author ibeeking
 * @Date 2021-01-27 18:50
 **/
@Service
public class GroupAutoTagsServiceImpl extends ServiceImpl<GroupTagsMapper, GroupTagsDO> implements IGroupAutoTagsService {

    @Resource
    private GroupTagsMapper groupTagsMapper;

    @Override
    public List<GroupAutoTagsVO> list(GroupAutoTagsQuery query) {
        List<GroupTagsDO> entityDOS = groupTagsMapper.selectList(new LambdaQueryWrapper<GroupTagsDO>()
                .eq(query.getId() != null, GroupTagsDO::getId, query.getId())
                .eq(GroupTagsDO::getType, 1)
                .like(StringUtils.isNotBlank(query.getTagsName()), GroupTagsDO::getTagsName, query.getTagsName())
                .orderByDesc(GroupTagsDO::getCreateTime)
        );
        List<GroupAutoTagsVO> entityVOS =JsonUtils.jsonToList(entityDOS, GroupAutoTagsVO.class);
        if(Collections3.isNotEmpty(entityVOS)){
            entityVOS.stream().forEach(d->{
               String tagsRule = d.getTagsRule();
               if(StringUtils.isNotEmpty(tagsRule)){
                   GroupAutoTagsRuleBO groupAutoTagsRuleBO = JsonUtils.fromJson(tagsRule, GroupAutoTagsRuleBO.class);
                   d.setTagsRuleBO(groupAutoTagsRuleBO);
               }
            });
        }
        return entityVOS;
    }

    @Override
    public Page<GroupAutoTagsVO> pageList(GroupAutoTagsPageQuery pageQuery) {
        LambdaQueryWrapper<GroupTagsDO> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(pageQuery.getId() != null, GroupTagsDO::getId, pageQuery.getId())
                .eq(GroupTagsDO::getType, 1)
                .like(StringUtils.isNotBlank(pageQuery.getTagsName()), GroupTagsDO::getTagsName, pageQuery.getTagsName())
                .orderByDesc(GroupTagsDO::getCreateTime);
        Page<GroupTagsDO> entityDOPages = groupTagsMapper.selectPage(new Page<GroupTagsDO>(pageQuery.getPageNum(), pageQuery.getPageSize()), wrapper);
        Page<GroupAutoTagsVO> entityVOPages = BeanUtil.convertPage(entityDOPages, GroupAutoTagsVO.class);
        if(entityVOPages!=null && entityVOPages.getSize()>0){
            List<GroupAutoTagsVO> GroupAutoTagsVOS = entityVOPages.getRecords();
            if(Collections3.isNotEmpty(GroupAutoTagsVOS)){
                GroupAutoTagsVOS.stream().forEach(d->{
                    String tagsRule = d.getTagsRule();
                    if(StringUtils.isNotEmpty(tagsRule)){
                        GroupAutoTagsRuleBO groupAutoTagsRuleBO = JsonUtils.fromJson(tagsRule, GroupAutoTagsRuleBO.class);
                        d.setTagsRuleBO(groupAutoTagsRuleBO);
                    }
                });
                entityVOPages.setRecords(GroupAutoTagsVOS);
            }
        }

        return entityVOPages;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean add(GroupAutoTagsParam param) {
        GroupTagsDO entityDO = BeanUtil.convertBean(param, GroupTagsDO.class);
        Long groupId = GenerateId.generateId();
        entityDO.setId(groupId);
        entityDO.setType(1);
        entityDO.setSort(groupId);
        entityDO.setStatus(0);
        entityDO.setCreateBy(UserUtils.getUserId());
        entityDO.setCreateTime(LocalDateTime.now());
        entityDO.setModifyTime(LocalDateTime.now());
        GroupAutoTagsRuleBO tagsRuleBO = param.getTagsRuleBO();
        if(tagsRuleBO != null){
            entityDO.setTagsRule(JsonUtils.toJsonStr(tagsRuleBO));
        }
        return groupTagsMapper.insert(entityDO) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean modify(GroupAutoTagsParam param) {
        if(param != null ) {
            GroupTagsDO entityDO = BeanUtil.convertBean(param, GroupTagsDO.class);
            entityDO.setType(1);
            entityDO.setStatus(0);
            entityDO.setModifyBy(UserUtils.getUserId());
            entityDO.setModifyTime(LocalDateTime.now());
            GroupAutoTagsRuleBO tagsRuleBO = param.getTagsRuleBO();
            if(tagsRuleBO != null){
                entityDO.setTagsRule(JsonUtils.toJsonStr(tagsRuleBO));
            }
            return groupTagsMapper.updateById(entityDO) > 0;
        }else{
            return false;
        }
    }

    @Override
    public GroupAutoTagsVO view(Long id) {
        GroupTagsDO entityDO = groupTagsMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        GroupAutoTagsVO entityVO = BeanUtil.convertBean(entityDO, GroupAutoTagsVO.class);
        String tagsRule = entityDO.getTagsRule();
        if(StringUtils.isNotEmpty(tagsRule)){
            GroupAutoTagsRuleBO groupAutoTagsRuleBO = JsonUtils.fromJson(tagsRule, GroupAutoTagsRuleBO.class);
            entityVO.setTagsRuleBO(groupAutoTagsRuleBO);
        }
        return entityVO;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean changeStatus(Long id, Integer status) {
        GroupTagsDO entityDO = groupTagsMapper.selectById(id);
        if(entityDO==null) throw new BusinessException("没有查到相关记录!");
        entityDO.setStatus(status);
        entityDO.setModifyBy(UserUtils.getUserId());
        entityDO.setModifyTime(LocalDateTime.now());
        return groupTagsMapper.updateById(entityDO) >0;
    }


    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean del(Long id) {
        GroupTagsDO entityDO = groupTagsMapper.selectById(id);
        if(entityDO == null) throw new BusinessException("没有查到相关记录！");
        return groupTagsMapper.deleteById(id) > 0;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Boolean updateBatchById(List<GroupTagsDO> entityList){
       Boolean reulst = updateBatchById(entityList);
       return reulst;
    }

}
